<?php
/**
 * PHPExcel
 *
 * Copyright (C) 2006 - 2012 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel
 * @copyright  Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
 * @version    1.7.7, 2012-05-19
 */

/** Error reporting */
error_reporting(E_ALL);

date_default_timezone_set('Asia/Shanghai');

/** Include PHPExcel */
require_once '../Classes/PHPExcel.php';
$sql="";
$name="";
if(!isset($_POST['option'])){

}elseif(!array_key_exists($_POST['option'], $reports)){
	
}


if(isset($_POST['targetYear'])){
	$option=$_POST['option'];
	$targetYear=$_POST['targetYear'];
	$targetMonth=$_POST['targetMonth'];
	$name=getLogicValue($option);
	$name.="($targetYear-$targetMonth)";
	$sql="CALL `$option` ('$targetYear','$targetMonth')";
}elseif(isset($_POST['fromYear'])){
	$option=$_POST['option'];
	$fromYear=$_POST['fromYear'];
	$fromMonth=$_POST['fromMonth'];
	$fromDate=$_POST['fromDate'];
	$toMonth=$_POST['toMonth'];
	$toYear=$_POST['toYear'];
	$toDate=$_POST['toDate'];
		$name=getLogicValue($option);
	$name.="($fromYear-$fromMonth-$fromDate 到 $toYear-$toMonth-$toDate)";
	$sql="CALL `$option` (
 	'$fromYear-$fromMonth-$fromDate',  '$toYear-$toMonth-$toDate'
	)";
}else{
	//error
}


		$qr = mysql_query( $sql ) or die( mysql_error() );
		
		
 




// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("菲雪洛琳服装有限公司")
							 ->setLastModifiedBy("菲雪洛琳服装有限公司")
							 ->setTitle("$name")
							 ->setSubject("$option")
							 ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
							 ->setKeywords("$option")
							 ->setCategory("$option");


		// Add some data
		$first = true;
		$col='A';
		$row=1;
	/*	$qrow = mysql_fetch_assoc( $qr );

 		foreach( $qrow as $k => $v )
        {
        	$objPHPExcel->setActiveSheetIndex(0)			
            ->setCellValue($col.$row, $k);
           $col=++$col;
    //  $k = mb_convert_encoding($k, 'UTF-16LE', 'UTF-8'); 
			
            // take the key and make label
            // make it uppper case and replace _ with ' '
            //xlsWriteLabel( $row, $col, strtoupper( ereg_replace( "_" , " " , $k ) ) );
			          //  xlsWriteLabel( $row, $col,  $k  );
            
        }
*/
while( $qrow = mysql_fetch_assoc( $qr ) )
{
    // Ok we are on the first row
    // lets make some headers of sorts
    if( $first )
    {
        foreach( $qrow as $k => $v )
        {
        	  $objPHPExcel->setActiveSheetIndex(0)			
            ->setCellValue($col.$row, $k);
           $col=++$col;
         }
 
        // prepare for the first real data row
		$col='A';
        $row++;
        $first = false;
    }
 
    // go through the data
    foreach( $qrow as $k => $v )
    {
        // write it out
        $objPHPExcel->setActiveSheetIndex(0)			
            ->setCellValue($col.$row, $v);
           $col=++$col;
     }
    // reset col and goto next row
	$col='A';
    $row++;
}

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$name.'.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

?>